package com.lihao.demo1.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lihao.demo1.R;


/**
 * Created by zengqiang on 2016/3/21.
 */
public class ImageTextButton extends FrameLayout {
    private FrameLayout mRootView;
    private TextView mTextSub;
    private ImageView mImageView;

    public ImageTextButton(Context context) {
        super(context);
        initView(context);
    }

    public ImageTextButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(context);
        mRootView = (FrameLayout) inflater.inflate(R.layout.layout_tab_view, null);
        mTextSub = (TextView) mRootView.findViewById(R.id.tv_text);
        mImageView = (ImageView) mRootView.findViewById(R.id.iv_icon);
        addView(mRootView);
    }

    public void setTextString(String s) {
        mTextSub.setText(s);
    }

    public void setButtonFocusable(boolean b) {
        //Log.d("UI", "setButtonFocusable---------b:" + b);
        if (b) {
            mTextSub.setVisibility(View.INVISIBLE);
//            mImageView.setScaleX(1);
//            mImageView.setScaleY(1);
            mImageView.setVisibility(VISIBLE);
        } else {
//            mTextSub.setScaleX(1);
//            mTextSub.setScaleY(1);
            mTextSub.setVisibility(View.VISIBLE);
            mImageView.setVisibility(INVISIBLE);
        }
    }

    public void setImageIcon(int imageIcon) {
        mImageView.setImageResource(imageIcon);
    }

    public void setAni(float value, boolean flag) {
        //右
        if (flag) {
            if (value < 0.5) {
                if (mImageView.getVisibility() != View.VISIBLE) {
                    mTextSub.setVisibility(View.GONE);
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setScaleX(0);
                    mImageView.setScaleY(0);
                }
                float temp = (0.5f - value) * 2;
                Log.d("UI", "1---------------------setAni: " + temp);
                mImageView.setScaleX(temp);
                mImageView.setScaleY(temp);
            } else {
                if (mTextSub.getVisibility() != View.VISIBLE) {
                    Log.d("UI", "setAni: ^^^^^^^^^^^^^^^^^^^^^^^");
                    mImageView.setVisibility(View.GONE);
                    mTextSub.setScaleX(0);
                    mTextSub.setScaleY(0);
                    mTextSub.setVisibility(View.VISIBLE);
                }
                float temp = (value - 0.5f) * 2;
                Log.d("UI", "2---------------------setAni: " + temp + "------------------:" + (mTextSub.getVisibility() == View.GONE));
                mTextSub.setScaleX(temp);
                mTextSub.setScaleY(temp);

            }
        } else {//左
            if (value < 0.5) {
                if (mImageView.getVisibility() == View.GONE) {
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setScaleX(0);
                    mImageView.setScaleY(0);
                }
                float temp = (0.5f - value) * 2;
                mImageView.setScaleX(temp);
                mImageView.setScaleY(temp);
            } else {
                if (mTextSub.getVisibility() == View.GONE) {
                    mTextSub.setVisibility(View.VISIBLE);
                    mTextSub.setScaleX(0);
                    mTextSub.setScaleY(0);
                }
                float temp = (value - 0.5f) * 2;
                mTextSub.setScaleX(temp);
                mTextSub.setScaleY(temp);

            }
        }
    }
}
